package com.css.fxfzdzzh.modules.system.service;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzdzzh.constants.FxfzConstants;
import com.css.fxfzdzzh.constants.SysUserRoleTypeEnum;
import com.css.fxfzdzzh.zcpt.sys.service.SysRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年04月21日  10:02
 */
@Service
public class SysServices {

    @Resource
    SysRoleService sysRoleService;

    public SysUserRoleTypeEnum getUserRoleType(String userId) {
        String role = sysRoleService.getRoleByUserId(userId);
        List<Map> maps = JSONObject.parseArray(role, Map.class);//一个人可能有多个角色
        //当前登录人所有的角色
        StringBuilder roles = new StringBuilder();
        for(int i=0;i<maps.size();i++){
            Map map = maps.get(i);
            roles.append(map.get("roleCode")).append(",");
        }
        String allRole = roles.toString();
        if(allRole.contains(FxfzConstants.DZZH_COUN_GL_ROLE) || allRole.contains(FxfzConstants.DZZH_COUN_YW_ROLE)){
            return SysUserRoleTypeEnum.DATA_ROLES_COUNTRY;
        }
        if(allRole.contains(FxfzConstants.DZZH_PRI_GL_ROLE) || allRole.contains(FxfzConstants.DZZH_PRI_YW_ROLE)) {
            return SysUserRoleTypeEnum.DATA_ROLES_PROVINCE;
        }
        return SysUserRoleTypeEnum.DATA_ROLES_OTHER;
    }

    public boolean userIsCountry(String userId) {
        return getUserRoleType(userId)==SysUserRoleTypeEnum.DATA_ROLES_COUNTRY;
    }

    public List<JSONObject> getRole(String userId) {
        String role = sysRoleService.getRoleByUserId(userId);
        return JSONObject.parseArray(role, JSONObject.class);
    }
}
